package com.common.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.swing.text.html.HTMLDocument.HTMLReader.FormAction;

/**
 * 
 * 功能描述:
 * 
 * @author Administrator
 * @Date Jul 19, 2008
 * @Time 9:46:11 AM
 * @version 1.0
 */
public class FileUtil {

	/**
	 * 功能描述:列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤
	 * 
	 * @param path
	 *            文件夹
	 */
	public static void list(File path) {
		if (!path.exists()) {
			System.out.println("文件名称不存在!");
		} else {
			if (path.isFile()) {
				if (path.getName().toLowerCase().endsWith(".pdf")
						|| path.getName().toLowerCase().endsWith(".doc")
						|| path.getName().toLowerCase().endsWith(".chm")
						|| path.getName().toLowerCase().endsWith(".html")
						|| path.getName().toLowerCase().endsWith(".htm")) {// 文件格式
					System.out.println(path);
					System.out.println(path.getName());
				}
			} else {
				File[] files = path.listFiles();
				for (int i = 0; i < files.length; i++) {
					list(files[i]);
				}
			}
		}
	}

	/**
	 * 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下
	 * 
	 * @param source
	 *            源文件
	 * @param target
	 *            目标文件路径
	 * @return void
	 */
	public static void copy(File source, File target) {
		File tarpath = new File(target, source.getName());
		if (source.isDirectory()) {
			tarpath.mkdir();
			File[] dir = source.listFiles();
			for (int i = 0; i < dir.length; i++) {
				copy(dir[i], tarpath);
			}
		} else {
			try {
				InputStream is = new FileInputStream(source); // 用于读取文件的原始字节流
				OutputStream os = new FileOutputStream(tarpath); // 用于写入文件的原始字节的流
				byte[] buf = new byte[1024];// 存储读取数据的缓冲区大小
				int len = 0;
				while ((len = is.read(buf)) != -1) {
					os.write(buf, 0, len);
				}
				is.close();
				os.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		File file = new File("D:\\个人资料\\MySQL 5");
//		list(file);
		Date myDate = new Date(); 
		DateFormat df = DateFormat.getDateInstance();
		System.out.println(df.format(myDate)); 
	}

}
最近下载更多
姜广坤  LV14 2022年5月20日
冰枫xjz8  LV31 2020年12月22日
xuyongff  LV24 2020年11月27日
460369476  LV1 2020年8月3日
moomin709  LV24 2020年7月6日
ma406805131  LV11 2020年6月18日
13043860zj  LV16 2020年1月9日
2141853180  LV5 2019年12月9日
JoyKinG  LV19 2019年8月15日
托马斯奎  LV6 2019年8月5日
最近浏览更多
1358849392  LV21 2023年5月9日
qq1176648922  LV6 2022年10月24日
姜广坤  LV14 2022年5月20日
crosa_Don  LV18 2022年4月1日
duqiangedu  LV3 2021年12月16日
kane666 2021年12月13日
暂无贡献等级
zhuiyunyiye  LV12 2021年12月13日
wuhaigang  LV9 2021年10月20日
王雪冰 2021年10月20日
暂无贡献等级
葡萄吃西瓜  LV4 2021年10月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友